import permissions from '../../api/permissions'

import {
  SHOW_SUCCESS_MSG,
  SHOW_ERROR_MSG,
  SET_PERMISSIONS,
  INSERT_PERMISSION,
  UPDATE_PERMISSION,
  DELETE_PERMISSION,
  SET_PERMISSIONS_WITH_ROLE,
  SET_PERMISSION_REFRESH_COUNT
} from '../mutation-types'

// initial state
const state = {
  permissions: [],
  permissionsWithRole: [], //携带角色授权的许可列表
  refreshCount: 0, //页面刷新计数
}

// getters
const getters = {
  getPermissions: state => state.permissions,
  getPermissionsWithRole: state => state.permissions,
}

// actions
const actions = {
  //请求资源权限列表
  reqGetPermissions({
    commit,
    state
  }, payload) {
    commit(SET_PERMISSIONS, {
      permissions: []
    })
    permissions.getPermissions(payload,
      (result) => {
        // debugger;
        if (result.states == 200) {
          commit(SET_PERMISSIONS, {
            permissions: result.results.permissions
          })
        }
      },
      (err) => {
        console.log(err);
      }
    )
  },
  //请求插入资源权限
  reqInsertPermission({
    commit,
    state
  }, payload) {
    // debugger;
    permissions.insertPermission(payload,
      (result) => {
        // debugger;
        if (result.states == 200) {
          commit(SHOW_SUCCESS_MSG, {
            message: '保存资源权限信息成功。',
            duration: 1000,
            needGoBack: true,
          });
        } else if (result.states == 500) {
          commit(SHOW_ERROR_MSG, {
            message: '系统内部错误，请联系系统管理员。'
          });
        }
      },
      (err) => {
        console.log(err);
      }
    )
  },
  //请求更新资源权限
  reqUpdatePermission({
    commit,
    state
  }, payload) {
    permissions.updatePermission(payload,
      (result) => {
        // debugger;
        if (result.states == 200) {
          commit(SHOW_SUCCESS_MSG, {
            message: '保存资源权限信息成功。',
            duration: 1000,
            needGoBack: true,
          });
        } else if (result.states == 500) {
          commit(SHOW_ERROR_MSG, {
            message: '系统内部错误，请联系系统管理员。'
          });
        }
      },
      (err) => {
        console.log(err);
      }
    )
  },
  //请求删除资源权限
  reqDeletePermission({
    commit,
    state
  }, payload) {
    permissions.deletePermission(payload,
      (result) => {
        // debugger;
        if (result.states == 200) {
          commit(DELETE_PERMISSION, payload.permissionId);
          commit(SHOW_SUCCESS_MSG, {
            message: '删除资源权限成功。',
            duration: 1000
          });
        } else if (result.states == 500) {
          commit(SHOW_ERROR_MSG, {
            message: '系统内部错误，请联系系统管理员。'
          });
        }
      },
      (err) => {
        console.log(err);
      }
    )
  },
  reqGetPermissionsWithRole({
    commit,
    state
  }, payload) {
    permissions.getPermissions(payload,
      (result) => {
        if (result.states == 200) {
          var permissionsInSystem = result.results.permissions;
          // debugger;
          console.log(permissionsInSystem);
          permissions.getPermissionsWithRole(payload,
            (result) => {
              if (result.states == 200) {
                var permissionsInRole = result.results;

                console.log(permissionsInSystem);
                console.log(permissionsInRole);
                var permissionsWithRole = permissionsInSystem.map(item => {
                  var authorised = false; //已授权的
                  for (var row of permissionsInRole) {
                    if (row._id === item._id) authorised = true;
                  }
                  item.authorised = authorised;
                  return item;
                });
                console.log(permissionsWithRole);
                commit(SET_PERMISSIONS_WITH_ROLE, {
                  permissionsWithRole
                })
              }
            },
            (err) => {
              console.log(err);
            }
          )
        }
      },
      (err) => {
        console.log(err);
      }
    )
  },
  //请求更新角色访问许可组
  reqUpdatePermissionsWithRole({
    commit,
    state
  }, payload) {
    permissions.updatePermissionsWithRole(payload,
      (result) => {
        // debugger;
        if (result.states == 200) {
          commit(SET_PERMISSION_REFRESH_COUNT);
          commit(SHOW_SUCCESS_MSG, {
            message: '保存角色访问许可组成功。',
            duration: 1000
          });
        } else if (result.states == 500) {
          commit(SHOW_ERROR_MSG, {
            message: '系统内部错误，请联系系统管理员。'
          });
        }
      },
      (err) => {
        console.log(err);
      }
    )
  }
}

// mutations
const mutations = {
  [SET_PERMISSIONS](state, {
    permissions
  }) {
    state.permissions = permissions
  },
  [UPDATE_PERMISSION](state, user) {
    //TODO??
  },
  [INSERT_PERMISSION](state, user) {
    //TODO??
  },
  [DELETE_PERMISSION](stete, _id) {
    state.permissions = state.permissions.filter(permission => permission._id !== _id);
  },
  [SET_PERMISSIONS_WITH_ROLE](state, {
    permissionsWithRole
  }) {
    state.permissionsWithRole = permissionsWithRole
  },
  [SET_PERMISSION_REFRESH_COUNT](state) {
    state.refreshCount = state.refreshCount + 1;
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
